<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/img/icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/img/icon.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="cayun's blog, IT, ACM/ICPC, blog, cayun" />










<meta property="og:type" content="website">
<meta property="og:title" content="cayun&#39;s blog">
<meta property="og:url" content="http://localhost/page/4/index.html">
<meta property="og:site_name" content="cayun&#39;s blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="cayun&#39;s blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://localhost/page/4/"/>





  <title>cayun's blog</title>
  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-93052608-1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1ca7c9c8011b37b0c209724dc77c24db";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">cayun's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">不积跬步, 无以至千里!</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/codeforces24D/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/codeforces24D/" itemprop="url">codeforces 24D Broken robot (概率DP+三对角矩阵)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-19T00:00:00+08:00">
                2017-07-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ACM-ICPC/" itemprop="url" rel="index">
                    <span itemprop="name">ACM/ICPC</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/codeforces24D/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/codeforces24D/" class="cy_cmt_count" data-xid="codeforces24D/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>设$dp<em>{i,j}$表示从$(i,j)$到第$n$行的期望长度。<br>则$dp</em>{i,j}=\frac{dp<em>{i,j}}{4}+\frac{dp</em>{i+1,j}}{4}+\frac{dp<em>{i,j-1}}{4}+\frac{dp</em>{i,j+1}}{4}+1, 1 &lt; j &lt; m$<br>不难化简得到$-dp<em>{i,j-1}+3dp</em>{i,j}-dp<em>{i,j+1}=dp</em>{i+1,j}+4$<br>对于$j=1​$和$j=m​$也是同样得出，需要注意的是$m=1​$的情况。<br>整个方程式刚好满足三对角矩阵的形式。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/codeforces24D/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/uva12338/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/uva12338/" itemprop="url">uva 12338 Anti-Rhyme Pairs (后缀数组+RMQ || Hash+二分)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-16T00:00:00+08:00">
                2017-07-16
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/uva12338/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/uva12338/" class="cy_cmt_count" data-xid="uva12338/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法一：后缀数组-RMQ"><a href="#解法一：后缀数组-RMQ" class="headerlink" title="解法一：后缀数组+RMQ"></a>解法一：后缀数组+RMQ</h1><p>首先将$n$个字符串连在一起，中间用大于$’z’$的$n-1$个互不相同的字符(实际要用int，不然会爆char的范围)隔开。然后根据后缀数组height的定义，可以知道，假设$p<em>{a}$代表$a$字符串首字母的位置，同理$p</em>{b}$代表$b$字符串首字母的位置，此处不妨设$rank<em>{p</em>{a}} &lt; rank<em>{p</em>{b}}$。然后只需要求出$rank<em>{p</em>{a}} + 1$到$rank<em>{p</em>{b}}$之间的最小height值，即为$a$和$b$的最长公共前缀。对于$a=b$的情况特别判断一下即可。</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1100010</span>;</div><div class="line"></div><div class="line"><span class="keyword">int</span> sa[N * <span class="number">3</span>], rnk[N], height[N], r[N];</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb))</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> G(x) ((x) &lt; tb ? (x) * 3 + 1 : ((x) - tb) * 3 + 2)</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">SuffixArray</span> &#123;</span></div><div class="line">    <span class="keyword">enum</span> &#123;MAXN = <span class="number">1100010</span>&#125;;</div><div class="line">    <span class="keyword">int</span> wa[MAXN * <span class="number">3</span>], wb[MAXN * <span class="number">3</span>], wv[MAXN * <span class="number">3</span>], ws[MAXN * <span class="number">3</span>];</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">c0</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> r[a] == r[b] &amp;&amp; r[a+<span class="number">1</span>] == r[b+<span class="number">1</span>] &amp;&amp; r[a+<span class="number">2</span>] == r[b+<span class="number">2</span>];</div><div class="line">    &#125; </div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">c12</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">int</span> *r, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (k == <span class="number">2</span>) <span class="keyword">return</span> r[a] &lt; r[b] || r[a] == r[b] &amp;&amp; c12(<span class="number">1</span>, r, a + <span class="number">1</span>, b + <span class="number">1</span>);</div><div class="line">        <span class="keyword">else</span> <span class="keyword">return</span> r[a] &lt; r[b] || r[a] == r[b] &amp;&amp; wv[a+<span class="number">1</span>] &lt; wv[b+<span class="number">1</span>];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *a, <span class="keyword">int</span> *b, <span class="keyword">int</span> n, <span class="keyword">int</span> m)</span> </span>&#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) wv[i] = r[a[i]];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) ws[i] = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) ws[wv[i]]++;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; m; i++) ws[i] += ws[i<span class="number">-1</span>];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) b[--ws[wv[i]]] = a[i];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">dc3</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *sa, <span class="keyword">int</span> n, <span class="keyword">int</span> m)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> i, j, *rn = r + n, *san = sa + n, ta = <span class="number">0</span>, tb = (n + <span class="number">1</span>) / <span class="number">3</span>, tbc = <span class="number">0</span>, p;</div><div class="line">        r[n] = r[n+<span class="number">1</span>] = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (i % <span class="number">3</span> != <span class="number">0</span>) wa[tbc++] = i;</div><div class="line">        &#125;</div><div class="line">        sort(r + <span class="number">2</span>, wa, wb, tbc, m);</div><div class="line">        sort(r + <span class="number">1</span>, wb, wa, tbc, m);</div><div class="line">        sort(r, wa, wb, tbc, m);</div><div class="line">        <span class="keyword">for</span> (p = <span class="number">1</span>, rn[F(wb[<span class="number">0</span>])] = <span class="number">0</span>, i = <span class="number">1</span>; i &lt; tbc; i++)</div><div class="line">            rn[F(wb[i])] = c0(r, wb[i<span class="number">-1</span>], wb[i]) ? p - <span class="number">1</span> : p++;</div><div class="line">        <span class="keyword">if</span> (p &lt; tbc) dc3(rn, san, tbc, p);</div><div class="line">        <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; tbc; i++) san[rn[i]] = i;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; tbc; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (san[i] &lt; tb) wb[ta++] = san[i] * <span class="number">3</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span> (n % <span class="number">3</span> == <span class="number">1</span>) wb[ta++] = n - <span class="number">1</span>;</div><div class="line">        sort(r, wb, wa, ta, m);</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; tbc; i++) wv[wb[i] = G(san[i])] = i;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>, j = <span class="number">0</span>, p = <span class="number">0</span>; i &lt; ta &amp;&amp; j &lt; tbc; p++)</div><div class="line">            sa[p] = c12(wb[j] % <span class="number">3</span>, r, wa[i], wb[j]) ? wa[i++] : wb[j++];</div><div class="line">        <span class="keyword">for</span> (; i &lt; ta; p++) sa[p] = wa[i++];</div><div class="line">        <span class="keyword">for</span> (; j &lt; tbc; p++) sa[p] = wb[j++]; </div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">getHeight</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *sa, <span class="keyword">int</span> *rnk, <span class="keyword">int</span> *height, <span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> i, j, k = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++) rnk[sa[i]] = i;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; height[rnk[i++]] = k) &#123;</div><div class="line">            <span class="keyword">for</span> (k ? k-- : <span class="number">0</span>, j = sa[rnk[i] - <span class="number">1</span>]; r[i+k] == r[j+k]; k++);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;suffixArray;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> ST_SIZE = <span class="number">22</span>;</div><div class="line"><span class="keyword">char</span> s[N];</div><div class="line"><span class="keyword">int</span> p[N], len[N];</div><div class="line"><span class="keyword">int</span> st[N][ST_SIZE];</div><div class="line"></div><div class="line"><span class="comment">//a数组从0开始计数</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">initST</span><span class="params">(<span class="keyword">int</span> *a, <span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++)</div><div class="line">        st[i][<span class="number">0</span>] = a[i];</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; ST_SIZE; j++) &#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i + (<span class="number">1</span> &lt;&lt; j) &lt;= n; i++)</div><div class="line">            st[i][j] = min(st[i][j - <span class="number">1</span>], st[i + (<span class="number">1</span> &lt;&lt; (j - <span class="number">1</span>))][j - <span class="number">1</span>]);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">queryST</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> k = (<span class="keyword">int</span>)log2(r + <span class="number">1</span> - l + <span class="number">0.000001</span>);</div><div class="line">    <span class="keyword">return</span> min(st[l][k], st[r + <span class="number">1</span> - (<span class="number">1</span> &lt;&lt; k)][k]);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++) &#123;</div><div class="line">        <span class="keyword">int</span> n; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</div><div class="line">        <span class="keyword">int</span> m = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%s"</span>, s);</div><div class="line">            p[i] = m; len[i] = <span class="built_in">strlen</span>(s);</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; s[j] != <span class="number">0</span>; j++) r[m++] = s[j] - <span class="string">'a'</span> + <span class="number">1</span>;</div><div class="line">            r[m++] = i == n ? <span class="number">0</span> : i + <span class="number">26</span>;</div><div class="line">        &#125;</div><div class="line">        m--;</div><div class="line">        suffixArray.dc3(r, sa, m + <span class="number">1</span>, n + <span class="number">30</span>);</div><div class="line">        suffixArray.getHeight(r, sa, rnk, height, m);</div><div class="line"></div><div class="line">        initST(height, m + <span class="number">1</span>);</div><div class="line"></div><div class="line">        <span class="built_in">printf</span>(<span class="string">"Case %d:\n"</span>, kk);</div><div class="line">        <span class="keyword">int</span> q; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;q);</div><div class="line">        <span class="keyword">while</span> (q--) &#123;</div><div class="line">            <span class="keyword">int</span> a, b; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;a, &amp;b);</div><div class="line">            <span class="keyword">if</span> (a == b) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, len[a]);</div><div class="line">            <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, queryST(min(rnk[p[a]], rnk[p[b]]) + <span class="number">1</span>, max(rnk[p[a]], rnk[p[b]])));</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="解法二：Hash-二分"><a href="#解法二：Hash-二分" class="headerlink" title="解法二：Hash+二分"></a>解法二：Hash+二分</h1><p>对于每个字符串前缀进行Hash处理一下，然后对于每组询问，二分长度进行求解。</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> SEED1 = <span class="number">179</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> SEED2 = <span class="number">239</span>;</div><div class="line"><span class="built_in">string</span> s[N];</div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; hash1[N], hash2[N];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (hash1[a][l] == hash1[b][l] &amp;&amp; hash2[a][l] == hash2[b][l]) &#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= l; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (s[a][i] != s[b][i]) <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="built_in">std</span>::ios::sync_with_stdio(<span class="literal">false</span>);</div><div class="line">    <span class="built_in">std</span>::<span class="built_in">cin</span>.tie(<span class="number">0</span>);</div><div class="line"></div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">cin</span> &gt;&gt; T;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++) &#123;</div><div class="line">        <span class="keyword">int</span> n; <span class="built_in">cin</span> &gt;&gt; n;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">            hash1[i].clear(); hash2[i].clear();</div><div class="line">            <span class="built_in">cin</span> &gt;&gt; s[i];</div><div class="line">            <span class="keyword">int</span> h1 = <span class="number">0</span>, h2 = <span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; s[i].size(); j++) &#123;</div><div class="line">                h1 = h1 * SEED1 + s[i][j];</div><div class="line">                h2 = h2 * SEED2 + s[i][j];</div><div class="line">                hash1[i].push_back(h1);</div><div class="line">                hash2[i].push_back(h2);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"Case "</span> &lt;&lt; kk &lt;&lt; <span class="string">":\n"</span>;</div><div class="line">        <span class="keyword">int</span> q; <span class="built_in">cin</span> &gt;&gt; q;</div><div class="line">        <span class="keyword">while</span> (q--) &#123;</div><div class="line">            <span class="keyword">int</span> a, b; <span class="built_in">cin</span> &gt;&gt; a &gt;&gt; b;</div><div class="line">            <span class="keyword">int</span> ans = <span class="number">-1</span>;</div><div class="line">            <span class="keyword">int</span> l = <span class="number">0</span>, r = min((<span class="keyword">int</span>)hash1[a].size(), (<span class="keyword">int</span>)hash2[a].size()) - <span class="number">1</span>;</div><div class="line">            <span class="keyword">while</span> (l &lt;= r) &#123;</div><div class="line">                <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>;</div><div class="line">                <span class="keyword">if</span> (judge(mid, a, b)) &#123;</div><div class="line">                    ans = mid; l = mid + <span class="number">1</span>;</div><div class="line">                &#125; <span class="keyword">else</span> &#123;</div><div class="line">                    r = mid - <span class="number">1</span>;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">            <span class="built_in">cout</span> &lt;&lt; ans + <span class="number">1</span> &lt;&lt; <span class="string">"\n"</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/gym101158E/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/gym101158E/" itemprop="url">Gym 101158E Infallibly Crack Perplexing Cryptarithm (模拟)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-14T00:00:00+08:00">
                2017-07-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/gym101158E/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/gym101158E/" class="cy_cmt_count" data-xid="gym101158E/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>一道复杂模拟题，看了别人的代码，感觉一瞬间发现了新大陆的感觉，就是那种1000行代码浓缩成100行的感觉。只能说好题。<br>原代码位置：<a href="http://natsugiri.hatenablog.com/entry/2016/10/23/234509" target="_blank" rel="external">http://natsugiri.hatenablog.com/entry/2016/10/23/234509</a></p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">char</span> s[<span class="number">50</span>], ss[<span class="number">50</span>];</div><div class="line"><span class="keyword">char</span> t[] = <span class="string">"01+-*()="</span>;</div><div class="line"><span class="keyword">int</span> id[<span class="number">128</span>];</div><div class="line"><span class="keyword">int</span> n, ans;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Result</span> &#123;</span></div><div class="line">    <span class="keyword">char</span> *p;</div><div class="line">    <span class="keyword">int</span> val;</div><div class="line">    Result(): p(<span class="number">0</span>), val(<span class="number">0</span>) &#123;&#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function">Result <span class="title">Q</span><span class="params">(<span class="keyword">char</span>*)</span></span>;</div><div class="line"><span class="function">Result <span class="title">E</span><span class="params">(<span class="keyword">char</span>*)</span></span>;</div><div class="line"><span class="function">Result <span class="title">T</span><span class="params">(<span class="keyword">char</span>*)</span></span>;</div><div class="line"><span class="function">Result <span class="title">F</span><span class="params">(<span class="keyword">char</span>*)</span></span>;</div><div class="line"><span class="function">Result <span class="title">N</span><span class="params">(<span class="keyword">char</span>*)</span></span>;</div><div class="line"></div><div class="line"><span class="function">Result <span class="title">Q</span><span class="params">(<span class="keyword">char</span> *s)</span> </span>&#123;</div><div class="line">    Result r = E(s);</div><div class="line">    <span class="keyword">if</span> (r.p == <span class="number">0</span>) <span class="keyword">return</span> r;</div><div class="line"></div><div class="line">    Result result;</div><div class="line">    <span class="keyword">if</span> (*(r.p) == <span class="string">'='</span>) &#123;</div><div class="line">        Result r2 = E(r.p + <span class="number">1</span>);</div><div class="line">        <span class="keyword">if</span> (r2.p == <span class="number">0</span>) <span class="keyword">return</span> result;</div><div class="line">        <span class="keyword">if</span> (*(r2.p) == <span class="number">0</span>) result.val = (<span class="keyword">int</span>)(r.val == r2.val);</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> result;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function">Result <span class="title">E</span><span class="params">(<span class="keyword">char</span> *s)</span> </span>&#123;</div><div class="line">    Result r = T(s);</div><div class="line">    <span class="keyword">if</span> (r.p == <span class="number">0</span>) <span class="keyword">return</span> r;</div><div class="line"></div><div class="line">    <span class="keyword">while</span> (*(r.p) == <span class="string">'+'</span> || *(r.p) == <span class="string">'-'</span>) &#123;</div><div class="line">        Result r2 = T(r.p + <span class="number">1</span>);</div><div class="line">        <span class="keyword">if</span> (r2.p == <span class="number">0</span>) <span class="keyword">return</span> r2;</div><div class="line">        *(r.p) == <span class="string">'+'</span> ? r.val += r2.val : r.val -= r2.val;</div><div class="line">        r.p = r2.p;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> r;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function">Result <span class="title">T</span><span class="params">(<span class="keyword">char</span> *s)</span> </span>&#123;</div><div class="line">    Result r = F(s);</div><div class="line">    <span class="keyword">if</span> (r.p == <span class="number">0</span>) <span class="keyword">return</span> r;</div><div class="line"></div><div class="line">    <span class="keyword">while</span> (*(r.p) == <span class="string">'*'</span>) &#123;</div><div class="line">        Result r2 = F(r.p + <span class="number">1</span>);</div><div class="line">        <span class="keyword">if</span> (r2.p == <span class="number">0</span>) <span class="keyword">return</span> r2;</div><div class="line">        r.val *= r2.val;</div><div class="line">        r.p = r2.p;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> r;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function">Result <span class="title">F</span><span class="params">(<span class="keyword">char</span> *s)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (*s == <span class="string">'-'</span>) &#123;</div><div class="line">        Result r = F(s + <span class="number">1</span>);</div><div class="line">        r.val = -r.val;</div><div class="line">        <span class="keyword">return</span> r;</div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (*s == <span class="string">'('</span>) &#123;</div><div class="line">        Result r = E(s + <span class="number">1</span>);</div><div class="line">        <span class="keyword">if</span> (r.p == <span class="number">0</span> || *(r.p) != <span class="string">')'</span>) <span class="keyword">return</span> Result();</div><div class="line">        (r.p)++;</div><div class="line">        <span class="keyword">return</span> r;</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="keyword">return</span> N(s);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function">Result <span class="title">N</span><span class="params">(<span class="keyword">char</span> *s)</span> </span>&#123;</div><div class="line">    Result r;</div><div class="line">    <span class="keyword">if</span> (*s != <span class="string">'0'</span> &amp;&amp; *s != <span class="string">'1'</span>) <span class="keyword">return</span> r;</div><div class="line">    <span class="keyword">if</span> (*s == <span class="string">'0'</span> &amp;&amp; (*(s+<span class="number">1</span>) == <span class="string">'0'</span> || *(s+<span class="number">1</span>) == <span class="string">'1'</span>)) <span class="keyword">return</span> r;</div><div class="line">    <span class="keyword">while</span> (*s == <span class="string">'0'</span> || *s == <span class="string">'1'</span>) &#123;</div><div class="line">        r.val = (r.val &lt;&lt; <span class="number">1</span>) + (*s - <span class="string">'0'</span>);</div><div class="line">        s++;</div><div class="line">    &#125;</div><div class="line">    r.p = s;</div><div class="line">    <span class="keyword">return</span> r;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">inSet</span><span class="params">(<span class="keyword">char</span> c, <span class="keyword">char</span> s[])</span> </span>&#123;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; s[i] != <span class="number">0</span>; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (c == s[i]) <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%s"</span>, s);</div><div class="line">    n = <span class="built_in">strlen</span>(s);</div><div class="line">    <span class="keyword">int</span> pos = <span class="number">0</span>;</div><div class="line">    <span class="built_in">memset</span>(id, <span class="number">-1</span>, <span class="keyword">sizeof</span> id);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (!inSet(s[i], t) &amp;&amp; id[s[i]] == <span class="number">-1</span>) &#123;</div><div class="line">            id[s[i]] = pos++;</div><div class="line">            <span class="keyword">if</span> (pos &gt; <span class="number">8</span>) &#123;</div><div class="line">                <span class="built_in">printf</span>(<span class="string">"0\n"</span>);</div><div class="line">                <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">int</span> ans = <span class="number">0</span>;</div><div class="line">    sort(t, t + <span class="number">8</span>);</div><div class="line">    <span class="keyword">do</span> &#123;</div><div class="line">        <span class="built_in">strcpy</span>(ss, s);</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (!inSet(ss[i], t)) ss[i] = t[id[ss[i]]];</div><div class="line">        &#125;</div><div class="line">        ans += Q(ss).val;</div><div class="line">    &#125; <span class="keyword">while</span> (next_permutation(t, t + <span class="number">8</span>));</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt;= <span class="number">8</span> - pos; i++) ans /= i;</div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/codeforces830A/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/codeforces830A/" itemprop="url">codeforces 830A Office Keys (二分)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-14T00:00:00+08:00">
                2017-07-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ACM-ICPC/" itemprop="url" rel="index">
                    <span itemprop="name">ACM/ICPC</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/codeforces830A/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/codeforces830A/" class="cy_cmt_count" data-xid="codeforces830A/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>首先将$a$和$b$数组进行排序，那么不难想象，从$b$数组中挑出$n$个数，那么肯定是按顺序匹配的最终答案最优。这一题的解法就是在这基础上进行二分答案。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/codeforces830A/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/codeforces827B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/codeforces827B/" itemprop="url">codeforces 827B High Load (构造)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-13T00:00:00+08:00">
                2017-07-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ACM-ICPC/" itemprop="url" rel="index">
                    <span itemprop="name">ACM/ICPC</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/codeforces827B/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/codeforces827B/" class="cy_cmt_count" data-xid="codeforces827B/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>可以构造一个星状的树，其中分支数量为$k$，每一个分支与其他分支的深度差值不超过$1$即可。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/codeforces827B/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/codeforces819B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/codeforces819B/" itemprop="url">codeforces 819B Mister B and PR Shifts (数学)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-12T00:00:00+08:00">
                2017-07-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ACM-ICPC/" itemprop="url" rel="index">
                    <span itemprop="name">ACM/ICPC</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/codeforces819B/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/codeforces819B/" class="cy_cmt_count" data-xid="codeforces819B/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>不知道怎么归类，暂且归类为数学吧。<br>假设对于$p_{i}$，我们当前移动次数为$j$，则我们不难列出下列式子：</p>
<ol>
<li>$p_{i} &gt;= i$时<ul>
<li>$f=p<em>{i}-(i+j)=-j+p</em>{i}-i, 0&lt;=j&lt;=p_{i}-i$</li>
<li>$f=(i+j)-p<em>{i}=j+i-p</em>{i}, p_{i}-i+1&lt;=j&lt;=n-i$</li>
<li>$f=p<em>{i}-(i+j-n)=-j+p</em>{i}+n-i, n-i+1&lt;=j&lt;=n-1$</li>
</ul>
</li>
<li>当$p_{i} &lt; i$时<ul>
<li>$f=(i+j)-p<em>{i}=j+i-p</em>{i}, 0&lt;=j&lt;=n-i$</li>
<li>$f=a<em>{i}-(i+j-n)=-j+a</em>{i}+n-i, n-i+1&lt;=j&lt;=n+p_{i}-i$</li>
<li>$f=(i+j-n)-a<em>{i}=j+i-n-a</em>{i}, n+p_{i}-i+1&lt;=j&lt;=n-1$</li>
</ul>
</li>
</ol>
<p>那么可以看出所有方程都可以转化为$f=kj+b$，这样通过前缀和就可以快速求出对每一个$j$，它所对应的$k$和$b$的值，然后最小的方程值就是我们需要的答案。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/codeforces819B/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/gym101170A/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/gym101170A/" itemprop="url">Gym 101170A Arranging Hat (动态规划)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-09T00:00:00+08:00">
                2017-07-09
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/gym101170A/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/gym101170A/" class="cy_cmt_count" data-xid="gym101170A/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>设$dp_{i,j}$表示前$i$个数，转换至多j次可以取得的最小数（指第$i$个数）。其中有一点需要注意的是，我们最多只要转换$2n$次便可以满足要求，而不是$n*m$次。中间的数字处理部分有些繁杂，可能会出现各种各样的小问题。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">40</span>;</div><div class="line"></div><div class="line"><span class="built_in">string</span> s[N+<span class="number">5</span>];</div><div class="line"><span class="built_in">string</span> dp[N+<span class="number">5</span>][N*<span class="number">2</span>+<span class="number">5</span>];</div><div class="line"><span class="keyword">bool</span> vis[N+<span class="number">5</span>][N*<span class="number">2</span>+<span class="number">5</span>];</div><div class="line"><span class="keyword">int</span> fat[N+<span class="number">5</span>][N*<span class="number">2</span>+<span class="number">5</span>];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="built_in">string</span> &amp;p, <span class="built_in">string</span> t, <span class="built_in">string</span> s, <span class="keyword">int</span> m, <span class="keyword">int</span> k)</span> </span>&#123;</div><div class="line">    p = s;</div><div class="line">    <span class="keyword">int</span> cnt = k, i;</div><div class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; m &amp;&amp; cnt &gt; <span class="number">0</span>; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (p[i] != t[i]) &#123;</div><div class="line">            p[i] = t[i];</div><div class="line">            cnt--;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (p &gt;= t) <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">    i--;</div><div class="line">    <span class="keyword">while</span> (i &gt;= <span class="number">0</span> &amp;&amp; p[i] == <span class="string">'9'</span>) i--;</div><div class="line">    <span class="keyword">if</span> (i &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">    p = s; cnt = k;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; i; j++) &#123;</div><div class="line">        <span class="keyword">if</span> (p[j] != t[j]) &#123;</div><div class="line">            p[j] = t[j];</div><div class="line">            cnt--;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (p[i] != t[i] + <span class="number">1</span>) &#123;</div><div class="line">        p[i] = t[i] + <span class="number">1</span>;</div><div class="line">        cnt--;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = i + <span class="number">1</span>; j &lt; m &amp;&amp; cnt &gt; <span class="number">0</span>; j++) &#123;</div><div class="line">        <span class="keyword">if</span> (p[j] != <span class="string">'0'</span>) &#123;</div><div class="line">            p[j] = <span class="string">'0'</span>;</div><div class="line">            cnt--;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> n, m; <span class="built_in">cin</span> &gt;&gt; n &gt;&gt; m;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) <span class="built_in">cin</span> &gt;&gt; s[i];</div><div class="line">    <span class="built_in">memset</span>(vis, <span class="literal">false</span>, <span class="keyword">sizeof</span> vis);</div><div class="line">    </div><div class="line">    dp[<span class="number">1</span>][<span class="number">0</span>] = s[<span class="number">1</span>];</div><div class="line">    vis[<span class="number">1</span>][<span class="number">0</span>] = <span class="literal">true</span>;</div><div class="line">    <span class="comment">//printf("dp[%d][%d]=%s\n", 1, 0, dp[1][0].c_str());</span></div><div class="line">    <span class="keyword">int</span> tn = <span class="number">0</span>;</div><div class="line">    <span class="built_in">string</span> t = s[<span class="number">1</span>];</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m &amp;&amp; tn &lt; n * <span class="number">2</span>; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (t[i] == <span class="string">'0'</span>) <span class="keyword">continue</span>;</div><div class="line">        t[i] = <span class="string">'0'</span>;</div><div class="line">        dp[<span class="number">1</span>][++tn] = t;</div><div class="line">        vis[<span class="number">1</span>][tn] = <span class="literal">true</span>;</div><div class="line">        <span class="comment">//printf("dp[%d][%d]=%s\n", 1, tn, dp[1][tn].c_str());</span></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++)</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt;= n * <span class="number">2</span>; j++) &#123;</div><div class="line">        <span class="keyword">if</span> (!vis[i][j]) <span class="keyword">continue</span>;</div><div class="line">        t = dp[i][j];</div><div class="line"></div><div class="line">        <span class="built_in">string</span> p = s[i+<span class="number">1</span>];</div><div class="line">        <span class="keyword">if</span> (p &gt;= t) &#123;</div><div class="line">            <span class="keyword">if</span> (!vis[i+<span class="number">1</span>][j] || dp[i+<span class="number">1</span>][j] &gt; p) &#123;</div><div class="line">                dp[i+<span class="number">1</span>][j] = p;</div><div class="line">                fat[i+<span class="number">1</span>][j] = j;</div><div class="line">                vis[i+<span class="number">1</span>][j] = <span class="literal">true</span>;</div><div class="line">                <span class="comment">//printf("dp[%d][%d]=%s, fat[%d][%d]=%d\n", i+1, j, dp[i+1][j].c_str(), i+1, j, fat[i+1][j]);</span></div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">1</span>; j + k &lt;= n * <span class="number">2</span>; k++) &#123;</div><div class="line">            <span class="keyword">if</span> (judge(p, dp[i][j], s[i+<span class="number">1</span>], m, k)) &#123;</div><div class="line">                <span class="keyword">if</span> (!vis[i+<span class="number">1</span>][j+k] || dp[i+<span class="number">1</span>][j+k] &gt; p) &#123;</div><div class="line">                    dp[i+<span class="number">1</span>][j+k] = p;</div><div class="line">                    vis[i+<span class="number">1</span>][j+k] = <span class="literal">true</span>;</div><div class="line">                    fat[i+<span class="number">1</span>][j+k] = j;</div><div class="line">                &#125; </div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">stack</span>&lt;<span class="built_in">string</span>&gt; ans;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= n * <span class="number">2</span>; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (vis[n][i]) &#123;</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = n; j &gt; <span class="number">0</span>; j--) &#123;</div><div class="line">                ans.push(dp[j][i]);</div><div class="line">                i = fat[j][i];</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">break</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">while</span> (!ans.empty()) &#123;</div><div class="line">        <span class="built_in">cout</span> &lt;&lt; ans.top() &lt;&lt; <span class="string">"\n"</span>;</div><div class="line">        ans.pop();</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/gym101170I/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/gym101170I/" itemprop="url">Gym 101170I Iron and Coal (搜索)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-08T00:00:00+08:00">
                2017-07-08
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/gym101170I/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/gym101170I/" class="cy_cmt_count" data-xid="gym101170I/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>其实仔细想想，这题的答案其实就是$min(dist<em>{t}+iron</em>{t}+coal<em>{t})$，其中$dist</em>{t}$表示$1$到$t$的最短距离，$iron<em>{t}$表示$t$到最近的铁矿的最短距离，$coal</em>{t}$表示$t$到最近的煤矿的最短距离。这个时候，你可能会想若是对于$t$而言，它到铁矿的最短路径和到煤矿的最短路径重合岂不是就会出现问题，但事实上并没有任何影响。举个简单的例子，假设煤矿在铁矿的路径上(反之同理)，设煤矿位置为$p$，那么不难想象$dist<em>{t}+iron</em>{t}+coal<em>{t} &gt;= ans</em>{t} &gt;= ans<em>{p} = dist</em>{p}+iron<em>{p}+coal</em>{p}$，而由于我们取的是最小值，所以我们最终的答案并不会受到影响。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</div><div class="line"><span class="keyword">int</span> dist[N]; <span class="comment">// 1到每个点的距离</span></div><div class="line"><span class="keyword">int</span> iron[N]; <span class="comment">// i到最近铁矿的距离</span></div><div class="line"><span class="keyword">int</span> coal[N]; <span class="comment">// i到最近煤矿的距离</span></div><div class="line"><span class="keyword">int</span> vis[N]; <span class="comment">// 判断每个点是否访问过</span></div><div class="line"><span class="keyword">bool</span> o[N]; <span class="comment">// 是否是铁矿</span></div><div class="line"><span class="keyword">bool</span> c[N]; <span class="comment">// 是否是煤矿</span></div><div class="line"><span class="keyword">int</span> hd[N], nxt[N * <span class="number">10</span>], to[N * <span class="number">10</span>];</div><div class="line"><span class="keyword">int</span> e = <span class="number">0</span>;</div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; g[N];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">insertEdge</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span> </span>&#123;</div><div class="line">    nxt[e] = hd[u];</div><div class="line">    hd[u] = e;</div><div class="line">    to[e] = v;</div><div class="line">    e++;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">bfsDist</span><span class="params">(<span class="keyword">int</span> s)</span> </span>&#123;</div><div class="line">    <span class="built_in">memset</span>(dist, <span class="number">-1</span>, <span class="keyword">sizeof</span> dist);</div><div class="line">    <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</div><div class="line">    q.push(s);</div><div class="line">    dist[s] = <span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span> (!q.empty()) &#123;</div><div class="line">        <span class="keyword">int</span> u = q.front(); q.pop();</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = hd[u]; i != <span class="number">-1</span>; i = nxt[i]) &#123;</div><div class="line">            <span class="keyword">if</span> (dist[to[i]] == <span class="number">-1</span>) &#123;</div><div class="line">                dist[to[i]] = dist[u] + <span class="number">1</span>;</div><div class="line">                q.push(to[i]);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">getResource</span><span class="params">(<span class="keyword">bool</span> r[N], <span class="keyword">int</span> d[N], <span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">    <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (r[i]) &#123;</div><div class="line">            d[i] = <span class="number">0</span>;</div><div class="line">            q.push(i);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">while</span> (!q.empty()) &#123;</div><div class="line">        <span class="keyword">int</span> u = q.front(); q.pop();</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; g[u].size(); i++) &#123;</div><div class="line">            <span class="keyword">int</span> v = g[u][i];</div><div class="line">            <span class="keyword">if</span> (d[v] == <span class="number">-1</span>) &#123;</div><div class="line">                d[v] = d[u] + <span class="number">1</span>;</div><div class="line">                q.push(v);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="built_in">memset</span>(hd, <span class="number">-1</span>, <span class="keyword">sizeof</span> hd);</div><div class="line">    <span class="built_in">memset</span>(o, <span class="literal">false</span>, <span class="keyword">sizeof</span> o);</div><div class="line">    <span class="built_in">memset</span>(c, <span class="literal">false</span>, <span class="keyword">sizeof</span> c);</div><div class="line">    <span class="built_in">memset</span>(iron, <span class="number">-1</span>, <span class="keyword">sizeof</span> iron);</div><div class="line">    <span class="built_in">memset</span>(coal, <span class="number">-1</span>, <span class="keyword">sizeof</span> coal);</div><div class="line"></div><div class="line">    <span class="keyword">int</span> n, m, k;</div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &amp;n, &amp;m, &amp;k);</div><div class="line">    <span class="keyword">int</span> x; </div><div class="line">    <span class="keyword">while</span> (m--) &#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;x);</div><div class="line">        o[x] = <span class="literal">true</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">while</span> (k--) &#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;x);</div><div class="line">        c[x] = <span class="literal">true</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> num;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;num);</div><div class="line">        <span class="keyword">while</span> (num--) &#123;</div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;x);</div><div class="line">            insertEdge(i, x);</div><div class="line">            g[x].push_back(i);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    bfsDist(<span class="number">1</span>);</div><div class="line">    getResource(o, iron, n);</div><div class="line">    getResource(c, coal, n);</div><div class="line"></div><div class="line">    <span class="keyword">int</span> ans = <span class="number">0x3f3f3f3f</span>;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (dist[i] != <span class="number">-1</span> &amp;&amp; iron[i] != <span class="number">-1</span> &amp;&amp; coal[i] != <span class="number">-1</span>) </div><div class="line">            ans = min(ans, dist[i] + iron[i] + coal[i]);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    ans == <span class="number">0x3f3f3f3f</span> ? <span class="built_in">puts</span>(<span class="string">"impossible"</span>) : <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/gym101170H/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/gym101170H/" itemprop="url">Gym 101170H Hamiltonian Hypercube (找规律)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-08T00:00:00+08:00">
                2017-07-08
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/gym101170H/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/gym101170H/" class="cy_cmt_count" data-xid="gym101170H/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>这题其实就是将格雷码转换成原来的二进制码即可，找一下规律就能知道怎么转换了。规律见代码。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> LL;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">70</span>;</div><div class="line"><span class="keyword">char</span> s[N], ss[N];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> n; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%s"</span>, s + <span class="number">1</span>);</div><div class="line">    ss[<span class="number">0</span>] = <span class="string">'0'</span>;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        ss[i] = ss[i<span class="number">-1</span>] == s[i] ? <span class="string">'0'</span> : <span class="string">'1'</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//ss[n+1] = 0;</span></div><div class="line">    <span class="comment">//puts(ss + 1);</span></div><div class="line">    LL a = <span class="number">0</span>, j = <span class="number">1L</span>L;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = n; i &gt; <span class="number">0</span>; i--) &#123;</div><div class="line">        a += (ss[i] - <span class="string">'0'</span>) * j;</div><div class="line">        j *= <span class="number">2L</span>L;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%s"</span>, s + <span class="number">1</span>);</div><div class="line">    ss[<span class="number">0</span>] = <span class="string">'0'</span>;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">        ss[i] = ss[i<span class="number">-1</span>] == s[i] ? <span class="string">'0'</span> : <span class="string">'1'</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    LL b = <span class="number">0</span>;</div><div class="line">    j = <span class="number">1L</span>L;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = n; i &gt; <span class="number">0</span>; i--) &#123;</div><div class="line">        b += (ss[i] - <span class="string">'0'</span>) * j;</div><div class="line">        j *= <span class="number">2L</span>L;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, b - a - <span class="number">1L</span>L);</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/codeforces822D/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/codeforces822D/" itemprop="url">codeforces 822D My pretty girl Noora (筛法)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-06T00:00:00+08:00">
                2017-07-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ACM-ICPC/" itemprop="url" rel="index">
                    <span itemprop="name">ACM/ICPC</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/codeforces822D/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/codeforces822D/" class="cy_cmt_count" data-xid="codeforces822D/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>可以很容易发现，我们每次只要尽可能往人少的组分，即分的组尽可能多就可以使得比较次数尽可能少。那么寻找最小的因子，需要$O(\sqrt{n})$的复杂度，需要求$n$个，那么就需要$O(n \sqrt{n})$的复杂度，很明显无法满足题目的要求，那么稍微思考一下，可以发现这个题目和筛法很像，于是稍微修改一下筛法便可以通过了。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/codeforces822D/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/3/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/3/">3</a><span class="page-number current">4</span><a class="page-number" href="/page/5/">5</a><span class="space">&hellip;</span><a class="page-number" href="/page/11/">11</a><a class="extend next" rel="next" href="/page/5/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/img/head.png"
                alt="cayun" />
            
              <p class="site-author-name" itemprop="name">cayun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">102</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">79</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/comeacrossyun" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:comeacrossyun@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">cayun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a></div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  




  
    <script id="cy_cmt_num" src="https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId=cyt6VLWhj"></script>
  









  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
